<template>
  <el-dialog
    v-model="visible"
    title="物流详情"
    width="486"
    align-center
    :show-close="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clickClose"
  >
    <div class="logis-detail">
      <div class="file-wrap">
        <p>物流凭证：</p>
        <div class="code-wrap">
          <div class="code mb10 ml10" v-for="(file, index) in fileUrls" :key="file">
            <img :src="getIcon(file)" @click="download(file)" />
          </div>
        </div>
      </div>

      <el-timeline style="max-width: 600px" v-if="activities.length > 0">
        <el-timeline-item v-for="(activity, index) in activities" :key="index">
          <template #default>
            <div class="left">
              <p>{{ activity.time.split(" ")[0] || "-" }}</p>
              <i>{{ activity.time.split(" ")[1] || "-" }}</i>
            </div>
            <div class="right">
              <p>{{ activity.context || "-" }}</p>
            </div>
          </template>
          <!-- {{ activity.content }} -->
        </el-timeline-item>
      </el-timeline>

      <div v-else v-loading="loading" style="display: flex; justify-content: center; width: 100%">
        <el-empty description="暂无数据" />
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, watch, onMounted } from "vue";
import { ElMessage } from "element-plus";
import word from "@/assets/images/word.png";
import pdf from "@/assets/images/pdf.png";
import video from "@/assets/images/video.png";
import excel from "@/assets/images/excel.png";

const visible = ref(false);
const emit = defineEmits(["close"]);
const props = defineProps({
  optionInfo: {
    type: Object,
    default: () => {
      return {
        isShow: false,
        detail: []
      };
    }
  }
});
const activities = ref([]);
const fileUrls = ref([]);
watch(
  () => props.optionInfo.isShow,
  async val => {
    visible.value = val;
  },
  { deep: true }
);
watch(
  () => props.optionInfo.detail,
  async val => {
    activities.value = val.data;
    fileUrls.value = val.logisticsCertificates ? val.logisticsCertificates.split(",") : [];
  },
  { deep: true }
);
onMounted(async () => {});

const download = url => {
  const link = document.createElement("a");
  link.href = url;
  link.click();
};

/**
 * 根据类型返回相应图标
 */
const getIcon = url => {
  let icon = "";
  if (getType(url) == "image") icon = url;
  else icon = icons[getType(url)];
  return icon;
};
const fileTypes = {
  image: ["jpg", "png", "jpeg", "gif"],
  video: ["mp4", "avi"],
  excel: ["xls", "xlsx"],
  word: ["doc", "docx"],
  pdf: ["pdf"]
};
const icons = { word, pdf, excel, video };
/**
 * 获取文件类型
 */
const getType = url => {
  let type = "";
  for (let key in fileTypes) {
    fileTypes[key]?.filter(v => {
      if (url.indexOf(v) != -1) {
        type = key;
      }
    });
  }
  return type;
};

const clickClose = () => {
  emit("close");
};
</script>

<style lang="scss" scoped>
.block {
  height: 10px;
  margin: 20px -24px;
  background-color: #eef1f4;
}
.logis-detail {
  padding-top: 30px;

  max-height: 550px;
  overflow-y: auto;
  .file-wrap {
    display: flex;
    p {
      width: 80px;
      padding-top: 20px;
    }
    .code-wrap {
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      img {
        width: 80px;
        height: 80px;
      }
    }
  }
  .code {
    width: 80px;
    height: 80px;
    background: #f2f3f5;
    position: relative;

    &:hover {
      .del {
        display: flex;
      }
    }

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  :deep(.el-timeline) {
    padding-left: 30%;
    .el-timeline-item__wrapper {
      padding-left: 18px;
      top: -2px;
      min-height: 100px;
    }
    .el-timeline-item__node--normal {
      height: 8px;
      left: 1px;
      width: 8px;
    }
    .left {
      width: 100px;
      // border: 1px solid #eee;
      position: absolute;
      left: -110px;
      text-align: right;
      p {
        font-size: 16px;
        color: rgba(0, 0, 0, 0.6);
      }
      i {
        font-style: normal;
        color: #86909c;
        font-size: 12px;
      }
    }
    .right {
      width: 100%;
      // border: 1px solid #eee;
    }
  }
}
</style>
